﻿using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Media;
using System.Windows.Navigation;
using Windows.Phone.Media.Capture;

namespace Flashlight
{
    public partial class MainPage : PhoneApplicationPage
    {
        bool IsFlashlightEnabled = false;
        AudioVideoCaptureDevice avDevice = null;
        IReadOnlyList<object> supportedCameraModes = null;
        // Constructor
        public MainPage()
        {
            InitializeComponent();
            var sensorLocation = CameraSensorLocation.Back;
            supportedCameraModes = AudioVideoCaptureDevice
                    .GetSupportedPropertyValues(sensorLocation, KnownCameraAudioVideoProperties.VideoTorchMode);

            if (!supportedCameraModes.ToList().Contains((UInt32)VideoTorchMode.On))
            {
                MessageBox.Show("No flashlight found on your phone");
                turnOnFlashlight.IsEnabled = false;
            }
            PhoneApplicationService.Current.UserIdleDetectionMode = IdleDetectionMode.Enabled;
            // Sample code to localize the ApplicationBar
            //BuildLocalizedApplicationBar();
        }

        private async void turnOnFlashlight_Click(object sender, RoutedEventArgs e)
        {
            var sensorLocation = CameraSensorLocation.Back;

            try
            {
                // get the AudioViceoCaptureDevice
                if (avDevice == null)
                {
                    avDevice = await AudioVideoCaptureDevice.OpenAsync(sensorLocation,
                    AudioVideoCaptureDevice.GetAvailableCaptureResolutions(sensorLocation).First());
                }

                // turn flashlight on

                if (IsFlashlightEnabled)
                {
                    avDevice.SetProperty(KnownCameraAudioVideoProperties.VideoTorchMode, VideoTorchMode.Off);
                    IsFlashlightEnabled = false;
                }
                else
                {
                    avDevice.SetProperty(KnownCameraAudioVideoProperties.VideoTorchMode, VideoTorchMode.On);
                    IsFlashlightEnabled = true;
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show("Błąd podczas wykonywania operacji, spróbuj ponownie");
            }
        }

        private void FlashPage_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/FlashPage.xaml", UriKind.Relative));
        }

        private void WhiteButton_Click(object sender, RoutedEventArgs e)
        {
            SetColor(Colors.White);
        }

        private void RedButton_Click(object sender, RoutedEventArgs e)
        {
            SetColor(Colors.Red);
        }

        private void GreenButton_Click(object sender, RoutedEventArgs e)
        {
            SetColor(Colors.Green);
        }

        private void YellowButton_Click(object sender, RoutedEventArgs e)
        {
            SetColor(Colors.Yellow);
        }

        private void BlueButton_Click(object sender, RoutedEventArgs e)
        {
            SetColor(Colors.Blue);
        }

        private void GrayButton_Click(object sender, RoutedEventArgs e)
        {
            SetColor(Colors.Gray);
        }

        private void LightBlueButton_Click(object sender, RoutedEventArgs e)
        {
            SetColor(Colors.Orange);
        }

        private void CyanButton_Click(object sender, RoutedEventArgs e)
        {
            SetColor(Colors.Cyan);
        }

        private void OrangeButton_Click(object sender, RoutedEventArgs e)
        {
            SetColor(Colors.Orange);
        }

        private void PurpleButton_Click(object sender, RoutedEventArgs e)
        {
            SetColor(Colors.Purple);
        }

        private void SetColor(Color background)
        {
            App.FlashLightBackground = background;
            FlashPage.Background = new SolidColorBrush(background);
        }

        private void DisableLockScreenCheckbox_Checked(object sender, RoutedEventArgs e)
        {
            PhoneApplicationService.Current.UserIdleDetectionMode = IdleDetectionMode.Disabled;
        }

        private void DisableLockScreenCheckbox_Unchecked(object sender, RoutedEventArgs e)
        {
            PhoneApplicationService.Current.UserIdleDetectionMode = IdleDetectionMode.Enabled;
        }

        // Sample code for building a localized ApplicationBar
        //private void BuildLocalizedApplicationBar()
        //{
        //    // Set the page's ApplicationBar to a new instance of ApplicationBar.
        //    ApplicationBar = new ApplicationBar();

        //    // Create a new button and set the text value to the localized string from AppResources.
        //    ApplicationBarIconButton appBarButton = new ApplicationBarIconButton(new Uri("/Assets/AppBar/appbar.add.rest.png", UriKind.Relative));
        //    appBarButton.Text = AppResources.AppBarButtonText;
        //    ApplicationBar.Buttons.Add(appBarButton);

        //    // Create a new menu item with the localized string from AppResources.
        //    ApplicationBarMenuItem appBarMenuItem = new ApplicationBarMenuItem(AppResources.AppBarMenuItemText);
        //    ApplicationBar.MenuItems.Add(appBarMenuItem);
        //}
    }
}